﻿<!DOCTYPE html>
<html class="theme theme-white">
<head>
<meta charset="utf-8">
<title>排序算法</title>
<link href="https://www.zybuluo.com/static/assets/template-theme-white.css" rel="stylesheet" media="screen">
<style type="text/css">

#wmd-preview h1  {
    color: #0077bb; /* 将标题改为蓝色 */
}</style>
</head>
<body class="theme theme-white">
<div id="wmd-preview" class="wmd-preview wmd-preview-full-reader"><div class="md-section-divider"></div><div class="md-section-divider"></div><h1 data-anchor-id="3bfv" id="排序算法">排序算法</h1><p data-anchor-id="vv2o"><code>Java面试题</code> <code>排序算法</code></p><hr><div class="md-section-divider"></div><h3 data-anchor-id="5la6" id="冒泡排序">冒泡排序</h3><p data-anchor-id="54ss">冒泡排序是最简单的排序之一了，其大体思想就是通过与相邻元素的比较和交换来把小的数交换到最前面。这个过程类似于水泡向上升一样，因此而得名。举个栗子，对5,3,8,6,4这个无序序列进行冒泡排序。首先从后向前冒泡，4和6比较，把4交换到前面，序列变成5,3,8,4,6。同理4和8交换，变成5,3,4,8,6,3和4无需交换。5和3交换，变成3,5,4,8,6,3.这样一次冒泡就完了，把最小的数3排到最前面了。对剩下的序列依次冒泡就会得到一个有序序列。冒泡排序的时间复杂度为O(n^2)。</p><p data-anchor-id="7u0g">实现代码：</p><div class="md-section-divider"></div><pre class="prettyprint linenums prettyprinted" data-anchor-id="5kgv"><ol class="linenums"><li class="L0"><code class="language-java"><span class="kwd">public</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">BubbleSort</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L1"><code class="language-java"></code></li><li class="L2"><code class="language-java"><span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> bubbleSort</span><span class="pun">(</span><span class="kwd">int</span><span class="pun">[]</span><span class="pln"> arr</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L3"><code class="language-java"><span class="pln">        </span><span class="kwd">if</span><span class="pun">(</span><span class="pln">arr </span><span class="pun">==</span><span class="pln"> </span><span class="kwd">null</span><span class="pln"> </span><span class="pun">||</span><span class="pln"> arr</span><span class="pun">.</span><span class="pln">length </span><span class="pun">==</span><span class="pln"> </span><span class="lit">0</span><span class="pun">)</span></code></li><li class="L4"><code class="language-java"><span class="pln">            </span><span class="kwd">return</span><span class="pln"> </span><span class="pun">;</span></code></li><li class="L5"><code class="language-java"><span class="pln">        </span><span class="kwd">for</span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> i</span><span class="pun">=</span><span class="lit">0</span><span class="pun">;</span><span class="pln"> i</span><span class="pun">&lt;</span><span class="pln">arr</span><span class="pun">.</span><span class="pln">length</span><span class="pun">-</span><span class="lit">1</span><span class="pun">;</span><span class="pln"> i</span><span class="pun">++)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L6"><code class="language-java"><span class="pln">            </span><span class="kwd">for</span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> j</span><span class="pun">=</span><span class="pln">arr</span><span class="pun">.</span><span class="pln">length</span><span class="pun">-</span><span class="lit">1</span><span class="pun">;</span><span class="pln"> j</span><span class="pun">&gt;</span><span class="pln">i</span><span class="pun">;</span><span class="pln"> j</span><span class="pun">--)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L7"><code class="language-java"><span class="pln">                </span><span class="kwd">if</span><span class="pun">(</span><span class="pln">arr</span><span class="pun">[</span><span class="pln">j</span><span class="pun">]</span><span class="pln"> </span><span class="pun">&lt;</span><span class="pln"> arr</span><span class="pun">[</span><span class="pln">j</span><span class="pun">-</span><span class="lit">1</span><span class="pun">])</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L8"><code class="language-java"><span class="pln">                    swap</span><span class="pun">(</span><span class="pln">arr</span><span class="pun">,</span><span class="pln"> j</span><span class="pun">-</span><span class="lit">1</span><span class="pun">,</span><span class="pln"> j</span><span class="pun">);</span></code></li><li class="L9"><code class="language-java"><span class="pln">                </span><span class="pun">}</span></code></li><li class="L0"><code class="language-java"><span class="pln">            </span><span class="pun">}</span></code></li><li class="L1"><code class="language-java"><span class="pln">        </span><span class="pun">}</span></code></li><li class="L2"><code class="language-java"><span class="pln">    </span><span class="pun">}</span></code></li><li class="L3"><code class="language-java"></code></li><li class="L4"><code class="language-java"></code></li><li class="L5"><code class="language-java"><span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> swap</span><span class="pun">(</span><span class="kwd">int</span><span class="pun">[]</span><span class="pln"> arr</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> i</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> j</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L6"><code class="language-java"><span class="pln">        </span><span class="kwd">int</span><span class="pln"> temp </span><span class="pun">=</span><span class="pln"> arr</span><span class="pun">[</span><span class="pln">i</span><span class="pun">];</span></code></li><li class="L7"><code class="language-java"><span class="pln">        arr</span><span class="pun">[</span><span class="pln">i</span><span class="pun">]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> arr</span><span class="pun">[</span><span class="pln">j</span><span class="pun">];</span></code></li><li class="L8"><code class="language-java"><span class="pln">        arr</span><span class="pun">[</span><span class="pln">j</span><span class="pun">]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> temp</span><span class="pun">;</span></code></li><li class="L9"><code class="language-java"><span class="pln">    </span><span class="pun">}</span></code></li><li class="L0"><code class="language-java"><span class="pun">}</span></code></li></ol></pre><div class="md-section-divider"></div><h3 data-anchor-id="899s" id="选择排序">选择排序</h3><p data-anchor-id="nkvk">选择排序的思想其实和冒泡排序有点类似，都是在一次排序后把最小的元素放到最前面。但是过程不同，冒泡排序是通过相邻的比较和交换。而选择排序是通过对整体的选择。举个栗子，对5,3,8,6,4这个无序序列进行简单选择排序，首先要选择5以外的最小数来和5交换，也就是选择3和5交换，一次排序后就变成了3,5,8,6,4.对剩下的序列一次进行选择和交换，最终就会得到一个有序序列。其实选择排序可以看成冒泡排序的优化，因为其目的相同，只是选择排序只有在确定了最小数的前提下才进行交换，大大减少了交换的次数。选择排序的时间复杂度为O(n^2)</p><p data-anchor-id="5nzz">实现代码：</p><div class="md-section-divider"></div><pre class="prettyprint linenums prettyprinted" data-anchor-id="4ypo"><ol class="linenums"><li class="L0"><code><span class="kwd">public</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">SelectSort</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L1"><code></code></li><li class="L2"><code><span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> selectSort</span><span class="pun">(</span><span class="kwd">int</span><span class="pun">[]</span><span class="pln"> arr</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L3"><code><span class="pln">        </span><span class="kwd">if</span><span class="pun">(</span><span class="pln">arr </span><span class="pun">==</span><span class="pln"> </span><span class="kwd">null</span><span class="pln"> </span><span class="pun">||</span><span class="pln"> arr</span><span class="pun">.</span><span class="pln">length </span><span class="pun">==</span><span class="pln"> </span><span class="lit">0</span><span class="pun">)</span></code></li><li class="L4"><code><span class="pln">            </span><span class="kwd">return</span><span class="pln"> </span><span class="pun">;</span></code></li><li class="L5"><code><span class="pln">        </span><span class="kwd">int</span><span class="pln"> minIndex </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span></code></li><li class="L6"><code><span class="pln">        </span><span class="kwd">for</span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> i</span><span class="pun">=</span><span class="lit">0</span><span class="pun">;</span><span class="pln"> i</span><span class="pun">&lt;</span><span class="pln">arr</span><span class="pun">.</span><span class="pln">length</span><span class="pun">-</span><span class="lit">1</span><span class="pun">;</span><span class="pln"> i</span><span class="pun">++)</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="com">//只需要比较n-1次</span></code></li><li class="L7"><code><span class="pln">            minIndex </span><span class="pun">=</span><span class="pln"> i</span><span class="pun">;</span></code></li><li class="L8"><code><span class="pln">            </span><span class="kwd">for</span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> j</span><span class="pun">=</span><span class="pln">i</span><span class="pun">+</span><span class="lit">1</span><span class="pun">;</span><span class="pln"> j</span><span class="pun">&lt;</span><span class="pln">arr</span><span class="pun">.</span><span class="pln">length</span><span class="pun">;</span><span class="pln"> j</span><span class="pun">++)</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="com">//从i+1开始比较，因为minIndex默认为i了，i就没必要比了。</span></code></li><li class="L9"><code><span class="pln">                </span><span class="kwd">if</span><span class="pun">(</span><span class="pln">arr</span><span class="pun">[</span><span class="pln">j</span><span class="pun">]</span><span class="pln"> </span><span class="pun">&lt;</span><span class="pln"> arr</span><span class="pun">[</span><span class="pln">minIndex</span><span class="pun">])</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L0"><code><span class="pln">                    minIndex </span><span class="pun">=</span><span class="pln"> j</span><span class="pun">;</span></code></li><li class="L1"><code><span class="pln">                </span><span class="pun">}</span></code></li><li class="L2"><code><span class="pln">            </span><span class="pun">}</span></code></li><li class="L3"><code></code></li><li class="L4"><code><span class="pln">            </span><span class="kwd">if</span><span class="pun">(</span><span class="pln">minIndex </span><span class="pun">!=</span><span class="pln"> i</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="com">//如果minIndex不为i，说明找到了更小的值，交换之。</span></code></li><li class="L5"><code><span class="pln">                swap</span><span class="pun">(</span><span class="pln">arr</span><span class="pun">,</span><span class="pln"> i</span><span class="pun">,</span><span class="pln"> minIndex</span><span class="pun">);</span></code></li><li class="L6"><code><span class="pln">            </span><span class="pun">}</span></code></li><li class="L7"><code><span class="pln">        </span><span class="pun">}</span></code></li><li class="L8"><code></code></li><li class="L9"><code><span class="pln">    </span><span class="pun">}</span></code></li><li class="L0"><code></code></li><li class="L1"><code><span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> swap</span><span class="pun">(</span><span class="kwd">int</span><span class="pun">[]</span><span class="pln"> arr</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> i</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> j</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L2"><code><span class="pln">        </span><span class="kwd">int</span><span class="pln"> temp </span><span class="pun">=</span><span class="pln"> arr</span><span class="pun">[</span><span class="pln">i</span><span class="pun">];</span></code></li><li class="L3"><code><span class="pln">        arr</span><span class="pun">[</span><span class="pln">i</span><span class="pun">]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> arr</span><span class="pun">[</span><span class="pln">j</span><span class="pun">];</span></code></li><li class="L4"><code><span class="pln">        arr</span><span class="pun">[</span><span class="pln">j</span><span class="pun">]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> temp</span><span class="pun">;</span></code></li><li class="L5"><code><span class="pln">    </span><span class="pun">}</span></code></li><li class="L6"><code></code></li><li class="L7"><code><span class="pun">}</span></code></li></ol></pre><div class="md-section-divider"></div><h3 data-anchor-id="g5kd" id="插入排序">插入排序</h3><p data-anchor-id="cz9v">插入排序不是通过交换位置而是通过比较找到合适的位置插入元素来达到排序的目的的。相信大家都有过打扑克牌的经历，特别是牌数较大的。在分牌时可能要整理自己的牌，牌多的时候怎么整理呢？就是拿到一张牌，找到一个合适的位置插入。这个原理其实和插入排序是一样的。举个栗子，对5,3,8,6,4这个无序序列进行简单插入排序，首先假设第一个数的位置时正确的，想一下在拿到第一张牌的时候，没必要整理。然后3要插到5前面，把5后移一位，变成3,5,8,6,4.想一下整理牌的时候应该也是这样吧。然后8不用动，6插在8前面，8后移一位，4插在5前面，从5开始都向后移一位。注意在插入一个数的时候要保证这个数前面的数已经有序。简单插入排序的时间复杂度也是O(n^2)。</p><p data-anchor-id="2gwu">实现代码：</p><div class="md-section-divider"></div><pre class="prettyprint linenums prettyprinted" data-anchor-id="m4mq"><ol class="linenums"><li class="L0"><code><span class="kwd">public</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">InsertSort</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L1"><code></code></li><li class="L2"><code><span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> insertSort</span><span class="pun">(</span><span class="kwd">int</span><span class="pun">[]</span><span class="pln"> arr</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L3"><code><span class="pln">        </span><span class="kwd">if</span><span class="pun">(</span><span class="pln">arr </span><span class="pun">==</span><span class="pln"> </span><span class="kwd">null</span><span class="pln"> </span><span class="pun">||</span><span class="pln"> arr</span><span class="pun">.</span><span class="pln">length </span><span class="pun">==</span><span class="pln"> </span><span class="lit">0</span><span class="pun">)</span></code></li><li class="L4"><code><span class="pln">            </span><span class="kwd">return</span><span class="pln"> </span><span class="pun">;</span></code></li><li class="L5"><code></code></li><li class="L6"><code><span class="pln">        </span><span class="kwd">for</span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> i</span><span class="pun">=</span><span class="lit">1</span><span class="pun">;</span><span class="pln"> i</span><span class="pun">&lt;</span><span class="pln">arr</span><span class="pun">.</span><span class="pln">length</span><span class="pun">;</span><span class="pln"> i</span><span class="pun">++)</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="com">//假设第一个数位置时正确的；要往后移，必须要假设第一个。</span></code></li><li class="L7"><code></code></li><li class="L8"><code><span class="pln">            </span><span class="kwd">int</span><span class="pln"> j </span><span class="pun">=</span><span class="pln"> i</span><span class="pun">;</span></code></li><li class="L9"><code><span class="pln">            </span><span class="kwd">int</span><span class="pln"> target </span><span class="pun">=</span><span class="pln"> arr</span><span class="pun">[</span><span class="pln">i</span><span class="pun">];</span><span class="pln"> </span><span class="com">//待插入的</span></code></li><li class="L0"><code></code></li><li class="L1"><code><span class="pln">            </span><span class="com">//后移</span></code></li><li class="L2"><code><span class="pln">            </span><span class="kwd">while</span><span class="pun">(</span><span class="pln">j </span><span class="pun">&gt;</span><span class="pln"> </span><span class="lit">0</span><span class="pln"> </span><span class="pun">&amp;&amp;</span><span class="pln"> target </span><span class="pun">&lt;</span><span class="pln"> arr</span><span class="pun">[</span><span class="pln">j</span><span class="pun">-</span><span class="lit">1</span><span class="pun">])</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L3"><code><span class="pln">                arr</span><span class="pun">[</span><span class="pln">j</span><span class="pun">]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> arr</span><span class="pun">[</span><span class="pln">j</span><span class="pun">-</span><span class="lit">1</span><span class="pun">];</span></code></li><li class="L4"><code><span class="pln">                j </span><span class="pun">--;</span></code></li><li class="L5"><code><span class="pln">            </span><span class="pun">}</span></code></li><li class="L6"><code></code></li><li class="L7"><code><span class="pln">            </span><span class="com">//插入 </span></code></li><li class="L8"><code><span class="pln">            arr</span><span class="pun">[</span><span class="pln">j</span><span class="pun">]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> target</span><span class="pun">;</span></code></li><li class="L9"><code><span class="pln">        </span><span class="pun">}</span></code></li><li class="L0"><code></code></li><li class="L1"><code><span class="pln">    </span><span class="pun">}</span></code></li><li class="L2"><code></code></li><li class="L3"><code><span class="pun">}</span></code></li></ol></pre><div class="md-section-divider"></div><h3 data-anchor-id="h80g" id="快速排序">快速排序</h3><p data-anchor-id="b48w">快速排序一听名字就觉得很高端，在实际应用当中快速排序确实也是表现最好的排序算法。快速排序虽然高端，但其实其思想是来自冒泡排序，冒泡排序是通过相邻元素的比较和交换把最小的冒泡到最顶端，而快速排序是比较和交换小数和大数，这样一来不仅把小数冒泡到上面同时也把大数沉到下面。</p><p data-anchor-id="nkzg">举个栗子：对5,3,8,6,4这个无序序列进行快速排序，思路是右指针找比基准数小的，左指针找比基准数大的，交换之。</p><p data-anchor-id="8eas">5,3,8,6,4 用5作为比较的基准，最终会把5小的移动到5的左边，比5大的移动到5的右边。</p><p data-anchor-id="yjbw">5,3,8,6,4 首先设置i,j两个指针分别指向两端，j指针先扫描（思考一下为什么？）4比5小停止。然后i扫描，8比5大停止。交换i,j位置。</p><p data-anchor-id="j8qv">5,3,4,6,8 然后j指针再扫描，这时j扫描4时两指针相遇。停止。然后交换4和基准数。</p><p data-anchor-id="njw2">4,3,5,6,8 一次划分后达到了左边比5小，右边比5大的目的。之后对左右子序列递归排序，最终得到有序序列。</p><p data-anchor-id="36uu">上面留下来了一个问题为什么一定要j指针先动呢？首先这也不是绝对的，这取决于基准数的位置，因为在最后两个指针相遇的时候，要交换基准数到相遇的位置。一般选取第一个数作为基准数，那么就是在左边，所以最后相遇的数要和基准数交换，那么相遇的数一定要比基准数小。所以j指针先移动才能先找到比基准数小的数。</p><p data-anchor-id="ni1z">快速排序是不稳定的，其时间平均时间复杂度是O(nlgn)。</p><p data-anchor-id="ckm1">实现代码：</p><div class="md-section-divider"></div><pre class="prettyprint linenums prettyprinted" data-anchor-id="4d90"><ol class="linenums"><li class="L0"><code><span class="kwd">public</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">QuickSort</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L1"><code><span class="pln">    </span><span class="com">//一次划分</span></code></li><li class="L2"><code><span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> partition</span><span class="pun">(</span><span class="kwd">int</span><span class="pun">[]</span><span class="pln"> arr</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> left</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> right</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L3"><code><span class="pln">        </span><span class="kwd">int</span><span class="pln"> pivotKey </span><span class="pun">=</span><span class="pln"> arr</span><span class="pun">[</span><span class="pln">left</span><span class="pun">];</span></code></li><li class="L4"><code><span class="pln">        </span><span class="kwd">int</span><span class="pln"> pivotPointer </span><span class="pun">=</span><span class="pln"> left</span><span class="pun">;</span></code></li><li class="L5"><code></code></li><li class="L6"><code><span class="pln">        </span><span class="kwd">while</span><span class="pun">(</span><span class="pln">left </span><span class="pun">&lt;</span><span class="pln"> right</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L7"><code><span class="pln">            </span><span class="kwd">while</span><span class="pun">(</span><span class="pln">left </span><span class="pun">&lt;</span><span class="pln"> right </span><span class="pun">&amp;&amp;</span><span class="pln"> arr</span><span class="pun">[</span><span class="pln">right</span><span class="pun">]</span><span class="pln"> </span><span class="pun">&gt;=</span><span class="pln"> pivotKey</span><span class="pun">)</span></code></li><li class="L8"><code><span class="pln">                right </span><span class="pun">--;</span></code></li><li class="L9"><code><span class="pln">            </span><span class="kwd">while</span><span class="pun">(</span><span class="pln">left </span><span class="pun">&lt;</span><span class="pln"> right </span><span class="pun">&amp;&amp;</span><span class="pln"> arr</span><span class="pun">[</span><span class="pln">left</span><span class="pun">]</span><span class="pln"> </span><span class="pun">&lt;=</span><span class="pln"> pivotKey</span><span class="pun">)</span></code></li><li class="L0"><code><span class="pln">                left </span><span class="pun">++;</span></code></li><li class="L1"><code><span class="pln">            swap</span><span class="pun">(</span><span class="pln">arr</span><span class="pun">,</span><span class="pln"> left</span><span class="pun">,</span><span class="pln"> right</span><span class="pun">);</span><span class="pln"> </span><span class="com">//把大的交换到右边，把小的交换到左边。</span></code></li><li class="L2"><code><span class="pln">        </span><span class="pun">}</span></code></li><li class="L3"><code><span class="pln">        swap</span><span class="pun">(</span><span class="pln">arr</span><span class="pun">,</span><span class="pln"> pivotPointer</span><span class="pun">,</span><span class="pln"> left</span><span class="pun">);</span><span class="pln"> </span><span class="com">//最后把pivot交换到中间</span></code></li><li class="L4"><code><span class="pln">        </span><span class="kwd">return</span><span class="pln"> left</span><span class="pun">;</span></code></li><li class="L5"><code><span class="pln">    </span><span class="pun">}</span></code></li><li class="L6"><code></code></li><li class="L7"><code><span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> quickSort</span><span class="pun">(</span><span class="kwd">int</span><span class="pun">[]</span><span class="pln"> arr</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> left</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> right</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L8"><code><span class="pln">        </span><span class="kwd">if</span><span class="pun">(</span><span class="pln">left </span><span class="pun">&gt;=</span><span class="pln"> right</span><span class="pun">)</span></code></li><li class="L9"><code><span class="pln">            </span><span class="kwd">return</span><span class="pln"> </span><span class="pun">;</span></code></li><li class="L0"><code><span class="pln">        </span><span class="kwd">int</span><span class="pln"> pivotPos </span><span class="pun">=</span><span class="pln"> partition</span><span class="pun">(</span><span class="pln">arr</span><span class="pun">,</span><span class="pln"> left</span><span class="pun">,</span><span class="pln"> right</span><span class="pun">);</span></code></li><li class="L1"><code><span class="pln">        quickSort</span><span class="pun">(</span><span class="pln">arr</span><span class="pun">,</span><span class="pln"> left</span><span class="pun">,</span><span class="pln"> pivotPos</span><span class="pun">-</span><span class="lit">1</span><span class="pun">);</span></code></li><li class="L2"><code><span class="pln">        quickSort</span><span class="pun">(</span><span class="pln">arr</span><span class="pun">,</span><span class="pln"> pivotPos</span><span class="pun">+</span><span class="lit">1</span><span class="pun">,</span><span class="pln"> right</span><span class="pun">);</span></code></li><li class="L3"><code><span class="pln">    </span><span class="pun">}</span></code></li><li class="L4"><code></code></li><li class="L5"><code><span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> sort</span><span class="pun">(</span><span class="kwd">int</span><span class="pun">[]</span><span class="pln"> arr</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L6"><code><span class="pln">        </span><span class="kwd">if</span><span class="pun">(</span><span class="pln">arr </span><span class="pun">==</span><span class="pln"> </span><span class="kwd">null</span><span class="pln"> </span><span class="pun">||</span><span class="pln"> arr</span><span class="pun">.</span><span class="pln">length </span><span class="pun">==</span><span class="pln"> </span><span class="lit">0</span><span class="pun">)</span></code></li><li class="L7"><code><span class="pln">            </span><span class="kwd">return</span><span class="pln"> </span><span class="pun">;</span></code></li><li class="L8"><code><span class="pln">        quickSort</span><span class="pun">(</span><span class="pln">arr</span><span class="pun">,</span><span class="pln"> </span><span class="lit">0</span><span class="pun">,</span><span class="pln"> arr</span><span class="pun">.</span><span class="pln">length</span><span class="pun">-</span><span class="lit">1</span><span class="pun">);</span></code></li><li class="L9"><code><span class="pln">    </span><span class="pun">}</span></code></li><li class="L0"><code></code></li><li class="L1"><code><span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> swap</span><span class="pun">(</span><span class="kwd">int</span><span class="pun">[]</span><span class="pln"> arr</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> left</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> right</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L2"><code><span class="pln">        </span><span class="kwd">int</span><span class="pln"> temp </span><span class="pun">=</span><span class="pln"> arr</span><span class="pun">[</span><span class="pln">left</span><span class="pun">];</span></code></li><li class="L3"><code><span class="pln">        arr</span><span class="pun">[</span><span class="pln">left</span><span class="pun">]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> arr</span><span class="pun">[</span><span class="pln">right</span><span class="pun">];</span></code></li><li class="L4"><code><span class="pln">        arr</span><span class="pun">[</span><span class="pln">right</span><span class="pun">]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> temp</span><span class="pun">;</span></code></li><li class="L5"><code><span class="pln">    </span><span class="pun">}</span></code></li><li class="L6"><code></code></li><li class="L7"><code><span class="pun">}</span></code></li></ol></pre><p data-anchor-id="mq18">其实上面的代码还可以再优化，上面代码中基准数已经在pivotKey中保存了，所以不需要每次交换都设置一个temp变量，在交换左右指针的时候只需要先后覆盖就可以了。这样既能减少空间的使用还能降低赋值运算的次数。优化代码如下：</p><div class="md-section-divider"></div><pre class="prettyprint linenums prettyprinted" data-anchor-id="3g7z"><ol class="linenums"><li class="L0"><code><span class="kwd">public</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">QuickSort</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L1"><code></code></li><li class="L2"><code><span class="pln">    </span><span class="com">/**</span></code></li><li class="L3"><code><span class="com">     * 划分</span></code></li><li class="L4"><code><span class="com">     * @param arr</span></code></li><li class="L5"><code><span class="com">     * @param left</span></code></li><li class="L6"><code><span class="com">     * @param right</span></code></li><li class="L7"><code><span class="com">     * @return</span></code></li><li class="L8"><code><span class="com">     */</span></code></li><li class="L9"><code><span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> partition</span><span class="pun">(</span><span class="kwd">int</span><span class="pun">[]</span><span class="pln"> arr</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> left</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> right</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L0"><code><span class="pln">        </span><span class="kwd">int</span><span class="pln"> pivotKey </span><span class="pun">=</span><span class="pln"> arr</span><span class="pun">[</span><span class="pln">left</span><span class="pun">];</span></code></li><li class="L1"><code></code></li><li class="L2"><code><span class="pln">        </span><span class="kwd">while</span><span class="pun">(</span><span class="pln">left </span><span class="pun">&lt;</span><span class="pln"> right</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L3"><code><span class="pln">            </span><span class="kwd">while</span><span class="pun">(</span><span class="pln">left </span><span class="pun">&lt;</span><span class="pln"> right </span><span class="pun">&amp;&amp;</span><span class="pln"> arr</span><span class="pun">[</span><span class="pln">right</span><span class="pun">]</span><span class="pln"> </span><span class="pun">&gt;=</span><span class="pln"> pivotKey</span><span class="pun">)</span></code></li><li class="L4"><code><span class="pln">                right </span><span class="pun">--;</span></code></li><li class="L5"><code><span class="pln">            arr</span><span class="pun">[</span><span class="pln">left</span><span class="pun">]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> arr</span><span class="pun">[</span><span class="pln">right</span><span class="pun">];</span><span class="pln"> </span><span class="com">//把小的移动到左边</span></code></li><li class="L6"><code><span class="pln">            </span><span class="kwd">while</span><span class="pun">(</span><span class="pln">left </span><span class="pun">&lt;</span><span class="pln"> right </span><span class="pun">&amp;&amp;</span><span class="pln"> arr</span><span class="pun">[</span><span class="pln">left</span><span class="pun">]</span><span class="pln"> </span><span class="pun">&lt;=</span><span class="pln"> pivotKey</span><span class="pun">)</span></code></li><li class="L7"><code><span class="pln">                left </span><span class="pun">++;</span></code></li><li class="L8"><code><span class="pln">            arr</span><span class="pun">[</span><span class="pln">right</span><span class="pun">]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> arr</span><span class="pun">[</span><span class="pln">left</span><span class="pun">];</span><span class="pln"> </span><span class="com">//把大的移动到右边</span></code></li><li class="L9"><code><span class="pln">        </span><span class="pun">}</span></code></li><li class="L0"><code><span class="pln">        arr</span><span class="pun">[</span><span class="pln">left</span><span class="pun">]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> pivotKey</span><span class="pun">;</span><span class="pln"> </span><span class="com">//最后把pivot赋值到中间</span></code></li><li class="L1"><code><span class="pln">        </span><span class="kwd">return</span><span class="pln"> left</span><span class="pun">;</span></code></li><li class="L2"><code><span class="pln">    </span><span class="pun">}</span></code></li><li class="L3"><code></code></li><li class="L4"><code><span class="pln">    </span><span class="com">/**</span></code></li><li class="L5"><code><span class="com">     * 递归划分子序列</span></code></li><li class="L6"><code><span class="com">     * @param arr</span></code></li><li class="L7"><code><span class="com">     * @param left</span></code></li><li class="L8"><code><span class="com">     * @param right</span></code></li><li class="L9"><code><span class="com">     */</span></code></li><li class="L0"><code><span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> quickSort</span><span class="pun">(</span><span class="kwd">int</span><span class="pun">[]</span><span class="pln"> arr</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> left</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> right</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L1"><code><span class="pln">        </span><span class="kwd">if</span><span class="pun">(</span><span class="pln">left </span><span class="pun">&gt;=</span><span class="pln"> right</span><span class="pun">)</span></code></li><li class="L2"><code><span class="pln">            </span><span class="kwd">return</span><span class="pln"> </span><span class="pun">;</span></code></li><li class="L3"><code><span class="pln">        </span><span class="kwd">int</span><span class="pln"> pivotPos </span><span class="pun">=</span><span class="pln"> partition</span><span class="pun">(</span><span class="pln">arr</span><span class="pun">,</span><span class="pln"> left</span><span class="pun">,</span><span class="pln"> right</span><span class="pun">);</span></code></li><li class="L4"><code><span class="pln">        quickSort</span><span class="pun">(</span><span class="pln">arr</span><span class="pun">,</span><span class="pln"> left</span><span class="pun">,</span><span class="pln"> pivotPos</span><span class="pun">-</span><span class="lit">1</span><span class="pun">);</span></code></li><li class="L5"><code><span class="pln">        quickSort</span><span class="pun">(</span><span class="pln">arr</span><span class="pun">,</span><span class="pln"> pivotPos</span><span class="pun">+</span><span class="lit">1</span><span class="pun">,</span><span class="pln"> right</span><span class="pun">);</span></code></li><li class="L6"><code><span class="pln">    </span><span class="pun">}</span></code></li><li class="L7"><code></code></li><li class="L8"><code><span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> sort</span><span class="pun">(</span><span class="kwd">int</span><span class="pun">[]</span><span class="pln"> arr</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L9"><code><span class="pln">        </span><span class="kwd">if</span><span class="pun">(</span><span class="pln">arr </span><span class="pun">==</span><span class="pln"> </span><span class="kwd">null</span><span class="pln"> </span><span class="pun">||</span><span class="pln"> arr</span><span class="pun">.</span><span class="pln">length </span><span class="pun">==</span><span class="pln"> </span><span class="lit">0</span><span class="pun">)</span></code></li><li class="L0"><code><span class="pln">            </span><span class="kwd">return</span><span class="pln"> </span><span class="pun">;</span></code></li><li class="L1"><code><span class="pln">        quickSort</span><span class="pun">(</span><span class="pln">arr</span><span class="pun">,</span><span class="pln"> </span><span class="lit">0</span><span class="pun">,</span><span class="pln"> arr</span><span class="pun">.</span><span class="pln">length</span><span class="pun">-</span><span class="lit">1</span><span class="pun">);</span></code></li><li class="L2"><code><span class="pln">    </span><span class="pun">}</span></code></li><li class="L3"><code></code></li><li class="L4"><code><span class="pun">}</span></code></li></ol></pre><p data-anchor-id="pgc2">总结快速排序的思想：冒泡+二分+递归分治，慢慢体会。。。</p><div class="md-section-divider"></div><h3 data-anchor-id="st3j" id="堆排序">堆排序</h3><p data-anchor-id="1i05">堆排序是借助堆来实现的选择排序，思想同简单的选择排序，以下以大顶堆为例。注意：如果想升序排序就使用大顶堆，反之使用小顶堆。原因是堆顶元素需要交换到序列尾部。</p><p data-anchor-id="dywt">首先，实现堆排序需要解决两个问题：</p><p data-anchor-id="8yom">如何由一个无序序列键成一个堆？</p><p data-anchor-id="d6s5">如何在输出堆顶元素之后，调整剩余元素成为一个新的堆？</p><p data-anchor-id="axcm">第一个问题，可以直接使用线性数组来表示一个堆，由初始的无序序列建成一个堆就需要自底向上从第一个非叶元素开始挨个调整成一个堆。</p><p data-anchor-id="f87j">第二个问题，怎么调整成堆？首先是将堆顶元素和最后一个元素交换。然后比较当前堆顶元素的左右孩子节点，因为除了当前的堆顶元素，左右孩子堆均满足条件，这时需要选择当前堆顶元素与左右孩子节点的较大者（大顶堆）交换，直至叶子节点。我们称这个自堆顶自叶子的调整成为筛选。</p><p data-anchor-id="xqbh">从一个无序序列建堆的过程就是一个反复筛选的过程。若将此序列看成是一个完全二叉树，则最后一个非终端节点是n/2取底个元素，由此筛选即可。举个栗子：</p><p data-anchor-id="qsxe">49,38,65,97,76,13,27,49序列的堆排序建初始堆和调整的过程如下： <br>
<img src="http://static.zybuluo.com/homiss/0l04yumri27ozq4md0o18zwc/image_1bk440a781tim1ffg1hqi12vb15ug9.png" alt="image_1bk440a781tim1ffg1hqi12vb15ug9.png-185.1kB" title=""></p><p data-anchor-id="k158"><img src="http://static.zybuluo.com/homiss/kepgtrsw1m38awytqpcniy9u/image_1bk440lq5i051859166rlru38m.png" alt="image_1bk440lq5i051859166rlru38m.png-229.6kB" title=""></p><p data-anchor-id="v9t9">实现代码：</p><div class="md-section-divider"></div><pre class="prettyprint linenums prettyprinted" data-anchor-id="7435"><ol class="linenums"><li class="L0"><code><span class="kwd">public</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">HeapSort</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L1"><code></code></li><li class="L2"><code><span class="pln">    </span><span class="com">/**</span></code></li><li class="L3"><code><span class="com">     * 堆筛选，除了start之外，start~end均满足大顶堆的定义。</span></code></li><li class="L4"><code><span class="com">     * 调整之后start~end称为一个大顶堆。</span></code></li><li class="L5"><code><span class="com">     * @param arr 待调整数组</span></code></li><li class="L6"><code><span class="com">     * @param start 起始指针</span></code></li><li class="L7"><code><span class="com">     * @param end 结束指针</span></code></li><li class="L8"><code><span class="com">     */</span></code></li><li class="L9"><code><span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> heapAdjust</span><span class="pun">(</span><span class="kwd">int</span><span class="pun">[]</span><span class="pln"> arr</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> start</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> </span><span class="kwd">end</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L0"><code><span class="pln">        </span><span class="kwd">int</span><span class="pln"> temp </span><span class="pun">=</span><span class="pln"> arr</span><span class="pun">[</span><span class="pln">start</span><span class="pun">];</span></code></li><li class="L1"><code></code></li><li class="L2"><code><span class="pln">        </span><span class="kwd">for</span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> i</span><span class="pun">=</span><span class="lit">2</span><span class="pun">*</span><span class="pln">start</span><span class="pun">+</span><span class="lit">1</span><span class="pun">;</span><span class="pln"> i</span><span class="pun">&lt;=</span><span class="kwd">end</span><span class="pun">;</span><span class="pln"> i</span><span class="pun">*=</span><span class="lit">2</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L3"><code><span class="pln">            </span><span class="com">//左右孩子的节点分别为2*i+1,2*i+2</span></code></li><li class="L4"><code></code></li><li class="L5"><code><span class="pln">            </span><span class="com">//选择出左右孩子较小的下标</span></code></li><li class="L6"><code><span class="pln">            </span><span class="kwd">if</span><span class="pun">(</span><span class="pln">i </span><span class="pun">&lt;</span><span class="pln"> </span><span class="kwd">end</span><span class="pln"> </span><span class="pun">&amp;&amp;</span><span class="pln"> arr</span><span class="pun">[</span><span class="pln">i</span><span class="pun">]</span><span class="pln"> </span><span class="pun">&lt;</span><span class="pln"> arr</span><span class="pun">[</span><span class="pln">i</span><span class="pun">+</span><span class="lit">1</span><span class="pun">])</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L7"><code><span class="pln">                i </span><span class="pun">++;</span><span class="pln"> </span></code></li><li class="L8"><code><span class="pln">            </span><span class="pun">}</span></code></li><li class="L9"><code><span class="pln">            </span><span class="kwd">if</span><span class="pun">(</span><span class="pln">temp </span><span class="pun">&gt;=</span><span class="pln"> arr</span><span class="pun">[</span><span class="pln">i</span><span class="pun">])</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L0"><code><span class="pln">                </span><span class="kwd">break</span><span class="pun">;</span><span class="pln"> </span><span class="com">//已经为大顶堆，=保持稳定性。</span></code></li><li class="L1"><code><span class="pln">            </span><span class="pun">}</span></code></li><li class="L2"><code><span class="pln">            arr</span><span class="pun">[</span><span class="pln">start</span><span class="pun">]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> arr</span><span class="pun">[</span><span class="pln">i</span><span class="pun">];</span><span class="pln"> </span><span class="com">//将子节点上移</span></code></li><li class="L3"><code><span class="pln">            start </span><span class="pun">=</span><span class="pln"> i</span><span class="pun">;</span><span class="pln"> </span><span class="com">//下一轮筛选</span></code></li><li class="L4"><code><span class="pln">        </span><span class="pun">}</span></code></li><li class="L5"><code></code></li><li class="L6"><code><span class="pln">        arr</span><span class="pun">[</span><span class="pln">start</span><span class="pun">]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> temp</span><span class="pun">;</span><span class="pln"> </span><span class="com">//插入正确的位置</span></code></li><li class="L7"><code><span class="pln">    </span><span class="pun">}</span></code></li><li class="L8"><code></code></li><li class="L9"><code></code></li><li class="L0"><code><span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> heapSort</span><span class="pun">(</span><span class="kwd">int</span><span class="pun">[]</span><span class="pln"> arr</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L1"><code><span class="pln">        </span><span class="kwd">if</span><span class="pun">(</span><span class="pln">arr </span><span class="pun">==</span><span class="pln"> </span><span class="kwd">null</span><span class="pln"> </span><span class="pun">||</span><span class="pln"> arr</span><span class="pun">.</span><span class="pln">length </span><span class="pun">==</span><span class="pln"> </span><span class="lit">0</span><span class="pun">)</span></code></li><li class="L2"><code><span class="pln">            </span><span class="kwd">return</span><span class="pln"> </span><span class="pun">;</span></code></li><li class="L3"><code></code></li><li class="L4"><code><span class="pln">        </span><span class="com">//建立大顶堆</span></code></li><li class="L5"><code><span class="pln">        </span><span class="kwd">for</span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> i</span><span class="pun">=</span><span class="pln">arr</span><span class="pun">.</span><span class="pln">length</span><span class="pun">/</span><span class="lit">2</span><span class="pun">;</span><span class="pln"> i</span><span class="pun">&gt;=</span><span class="lit">0</span><span class="pun">;</span><span class="pln"> i</span><span class="pun">--)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L6"><code><span class="pln">            heapAdjust</span><span class="pun">(</span><span class="pln">arr</span><span class="pun">,</span><span class="pln"> i</span><span class="pun">,</span><span class="pln"> arr</span><span class="pun">.</span><span class="pln">length</span><span class="pun">-</span><span class="lit">1</span><span class="pun">);</span></code></li><li class="L7"><code><span class="pln">        </span><span class="pun">}</span></code></li><li class="L8"><code></code></li><li class="L9"><code><span class="pln">        </span><span class="kwd">for</span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> i</span><span class="pun">=</span><span class="pln">arr</span><span class="pun">.</span><span class="pln">length</span><span class="pun">-</span><span class="lit">1</span><span class="pun">;</span><span class="pln"> i</span><span class="pun">&gt;=</span><span class="lit">0</span><span class="pun">;</span><span class="pln"> i</span><span class="pun">--)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L0"><code><span class="pln">            swap</span><span class="pun">(</span><span class="pln">arr</span><span class="pun">,</span><span class="pln"> </span><span class="lit">0</span><span class="pun">,</span><span class="pln"> i</span><span class="pun">);</span></code></li><li class="L1"><code><span class="pln">            heapAdjust</span><span class="pun">(</span><span class="pln">arr</span><span class="pun">,</span><span class="pln"> </span><span class="lit">0</span><span class="pun">,</span><span class="pln"> i</span><span class="pun">-</span><span class="lit">1</span><span class="pun">);</span></code></li><li class="L2"><code><span class="pln">        </span><span class="pun">}</span></code></li><li class="L3"><code></code></li><li class="L4"><code><span class="pln">    </span><span class="pun">}</span></code></li><li class="L5"><code></code></li><li class="L6"><code><span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> swap</span><span class="pun">(</span><span class="kwd">int</span><span class="pun">[]</span><span class="pln"> arr</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> i</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> j</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L7"><code><span class="pln">        </span><span class="kwd">int</span><span class="pln"> temp </span><span class="pun">=</span><span class="pln"> arr</span><span class="pun">[</span><span class="pln">i</span><span class="pun">];</span></code></li><li class="L8"><code><span class="pln">        arr</span><span class="pun">[</span><span class="pln">i</span><span class="pun">]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> arr</span><span class="pun">[</span><span class="pln">j</span><span class="pun">];</span></code></li><li class="L9"><code><span class="pln">        arr</span><span class="pun">[</span><span class="pln">j</span><span class="pun">]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> temp</span><span class="pun">;</span></code></li><li class="L0"><code><span class="pln">    </span><span class="pun">}</span></code></li><li class="L1"><code><span class="pun">}</span></code></li></ol></pre><div class="md-section-divider"></div><h3 data-anchor-id="w6j8" id="希尔排序">希尔排序</h3><p data-anchor-id="qyxl">希尔排序是插入排序的一种高效率的实现，也叫缩小增量排序。简单的插入排序中，如果待排序列是正序时，时间复杂度是O(n)，如果序列是基本有序的，使用直接插入排序效率就非常高。希尔排序就利用了这个特点。基本思想是：先将整个待排记录序列分割成为若干子序列分别进行直接插入排序，待整个序列中的记录基本有序时再对全体记录进行一次直接插入排序。</p><p data-anchor-id="kwww">举个栗子：</p><p data-anchor-id="tyf7"><img src="http://static.zybuluo.com/homiss/k05lv08otvtkqep6lg7gbcim/image_1bk441i0u1rjssh498k11fulu513.png" alt="image_1bk441i0u1rjssh498k11fulu513.png-130kB" title=""></p><p data-anchor-id="ufdn">从上述排序过程可见，希尔排序的特点是，子序列的构成不是简单的逐段分割，而是将某个相隔某个增量的记录组成一个子序列。如上面的例子，第一堂排序时的增量为5，第二趟排序的增量为3。由于前两趟的插入排序中记录的关键字是和同一子序列中的前一个记录的关键字进行比较，因此关键字较小的记录就不是一步一步地向前挪动，而是跳跃式地往前移，从而使得进行最后一趟排序时，整个序列已经做到基本有序，只要作记录的少量比较和移动即可。因此希尔排序的效率要比直接插入排序高。</p><p data-anchor-id="o7j2">希尔排序的分析是复杂的，时间复杂度是所取增量的函数，这涉及一些数学上的难题。但是在大量实验的基础上推出当n在某个范围内时，时间复杂度可以达到O(n^1.3)。</p><p data-anchor-id="xost">实现代码：</p><div class="md-section-divider"></div><pre class="prettyprint linenums prettyprinted" data-anchor-id="174g"><ol class="linenums"><li class="L0"><code><span class="kwd">public</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">ShellSort</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L1"><code></code></li><li class="L2"><code><span class="pln">    </span><span class="com">/**</span></code></li><li class="L3"><code><span class="com">     * 希尔排序的一趟插入</span></code></li><li class="L4"><code><span class="com">     * @param arr 待排数组</span></code></li><li class="L5"><code><span class="com">     * @param d 增量</span></code></li><li class="L6"><code><span class="com">     */</span></code></li><li class="L7"><code><span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> shellInsert</span><span class="pun">(</span><span class="kwd">int</span><span class="pun">[]</span><span class="pln"> arr</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> d</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L8"><code><span class="pln">        </span><span class="kwd">for</span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> i</span><span class="pun">=</span><span class="pln">d</span><span class="pun">;</span><span class="pln"> i</span><span class="pun">&lt;</span><span class="pln">arr</span><span class="pun">.</span><span class="pln">length</span><span class="pun">;</span><span class="pln"> i</span><span class="pun">++)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L9"><code><span class="pln">            </span><span class="kwd">int</span><span class="pln"> j </span><span class="pun">=</span><span class="pln"> i </span><span class="pun">-</span><span class="pln"> d</span><span class="pun">;</span></code></li><li class="L0"><code><span class="pln">            </span><span class="kwd">int</span><span class="pln"> temp </span><span class="pun">=</span><span class="pln"> arr</span><span class="pun">[</span><span class="pln">i</span><span class="pun">];</span><span class="pln">    </span><span class="com">//记录要插入的数据  </span></code></li><li class="L1"><code><span class="pln">            </span><span class="kwd">while</span><span class="pln"> </span><span class="pun">(</span><span class="pln">j</span><span class="pun">&gt;=</span><span class="lit">0</span><span class="pln"> </span><span class="pun">&amp;&amp;</span><span class="pln"> arr</span><span class="pun">[</span><span class="pln">j</span><span class="pun">]&gt;</span><span class="pln">temp</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">  </span><span class="com">//从后向前，找到比其小的数的位置   </span></code></li><li class="L2"><code><span class="pln">                arr</span><span class="pun">[</span><span class="pln">j</span><span class="pun">+</span><span class="pln">d</span><span class="pun">]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> arr</span><span class="pun">[</span><span class="pln">j</span><span class="pun">];</span><span class="pln">    </span><span class="com">//向后挪动  </span></code></li><li class="L3"><code><span class="pln">                j </span><span class="pun">-=</span><span class="pln"> d</span><span class="pun">;</span><span class="pln">  </span></code></li><li class="L4"><code><span class="pln">            </span><span class="pun">}</span><span class="pln">  </span></code></li><li class="L5"><code></code></li><li class="L6"><code><span class="pln">            </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">j </span><span class="pun">!=</span><span class="pln"> i </span><span class="pun">-</span><span class="pln"> d</span><span class="pun">)</span><span class="pln">    </span><span class="com">//存在比其小的数 </span></code></li><li class="L7"><code><span class="pln">                arr</span><span class="pun">[</span><span class="pln">j</span><span class="pun">+</span><span class="pln">d</span><span class="pun">]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> temp</span><span class="pun">;</span></code></li><li class="L8"><code></code></li><li class="L9"><code><span class="pln">        </span><span class="pun">}</span></code></li><li class="L0"><code><span class="pln">    </span><span class="pun">}</span></code></li><li class="L1"><code></code></li><li class="L2"><code><span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> shellSort</span><span class="pun">(</span><span class="kwd">int</span><span class="pun">[]</span><span class="pln"> arr</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L3"><code><span class="pln">        </span><span class="kwd">if</span><span class="pun">(</span><span class="pln">arr </span><span class="pun">==</span><span class="pln"> </span><span class="kwd">null</span><span class="pln"> </span><span class="pun">||</span><span class="pln"> arr</span><span class="pun">.</span><span class="pln">length </span><span class="pun">==</span><span class="pln"> </span><span class="lit">0</span><span class="pun">)</span></code></li><li class="L4"><code><span class="pln">            </span><span class="kwd">return</span><span class="pln"> </span><span class="pun">;</span></code></li><li class="L5"><code><span class="pln">        </span><span class="kwd">int</span><span class="pln"> d </span><span class="pun">=</span><span class="pln"> arr</span><span class="pun">.</span><span class="pln">length </span><span class="pun">/</span><span class="pln"> </span><span class="lit">2</span><span class="pun">;</span></code></li><li class="L6"><code><span class="pln">        </span><span class="kwd">while</span><span class="pun">(</span><span class="pln">d </span><span class="pun">&gt;=</span><span class="pln"> </span><span class="lit">1</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L7"><code><span class="pln">            shellInsert</span><span class="pun">(</span><span class="pln">arr</span><span class="pun">,</span><span class="pln"> d</span><span class="pun">);</span></code></li><li class="L8"><code><span class="pln">            d </span><span class="pun">/=</span><span class="pln"> </span><span class="lit">2</span><span class="pun">;</span></code></li><li class="L9"><code><span class="pln">        </span><span class="pun">}</span></code></li><li class="L0"><code><span class="pln">    </span><span class="pun">}</span></code></li><li class="L1"><code></code></li><li class="L2"><code><span class="pun">}</span></code></li></ol></pre><div class="md-section-divider"></div><h3 data-anchor-id="gatz" id="归并排序">归并排序</h3><p data-anchor-id="p2t1">归并排序是另一种不同的排序方法，因为归并排序使用了递归分治的思想，所以理解起来比较容易。其基本思想是，先递归划分子问题，然后合并结果。把待排序列看成由两个有序的子序列，然后合并两个子序列，然后把子序列看成由两个有序序列。。。。。倒着来看，其实就是先两两合并，然后四四合并。。。最终形成有序序列。空间复杂度为O(n)，时间复杂度为O(nlogn)。</p><p data-anchor-id="7y2d">举个栗子： <br>
<img src="http://static.zybuluo.com/homiss/yjuwkzldw505oorxartr2kru/image_1bk442k5t63t1e6d1gpv162nt8p1t.png" alt="image_1bk442k5t63t1e6d1gpv162nt8p1t.png-86.5kB" title=""></p><p data-anchor-id="pvoh">实现代码：</p><div class="md-section-divider"></div><pre class="prettyprint linenums prettyprinted" data-anchor-id="fnar"><ol class="linenums"><li class="L0"><code><span class="kwd">public</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">MergeSort</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L1"><code></code></li><li class="L2"><code><span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> mergeSort</span><span class="pun">(</span><span class="kwd">int</span><span class="pun">[]</span><span class="pln"> arr</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L3"><code><span class="pln">        mSort</span><span class="pun">(</span><span class="pln">arr</span><span class="pun">,</span><span class="pln"> </span><span class="lit">0</span><span class="pun">,</span><span class="pln"> arr</span><span class="pun">.</span><span class="pln">length</span><span class="pun">-</span><span class="lit">1</span><span class="pun">);</span></code></li><li class="L4"><code><span class="pln">    </span><span class="pun">}</span></code></li><li class="L5"><code></code></li><li class="L6"><code><span class="pln">    </span><span class="com">/**</span></code></li><li class="L7"><code><span class="com">     * 递归分治</span></code></li><li class="L8"><code><span class="com">     * @param arr 待排数组</span></code></li><li class="L9"><code><span class="com">     * @param left 左指针</span></code></li><li class="L0"><code><span class="com">     * @param right 右指针</span></code></li><li class="L1"><code><span class="com">     */</span></code></li><li class="L2"><code><span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> mSort</span><span class="pun">(</span><span class="kwd">int</span><span class="pun">[]</span><span class="pln"> arr</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> left</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> right</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L3"><code><span class="pln">        </span><span class="kwd">if</span><span class="pun">(</span><span class="pln">left </span><span class="pun">&gt;=</span><span class="pln"> right</span><span class="pun">)</span></code></li><li class="L4"><code><span class="pln">            </span><span class="kwd">return</span><span class="pln"> </span><span class="pun">;</span></code></li><li class="L5"><code><span class="pln">        </span><span class="kwd">int</span><span class="pln"> mid </span><span class="pun">=</span><span class="pln"> </span><span class="pun">(</span><span class="pln">left </span><span class="pun">+</span><span class="pln"> right</span><span class="pun">)</span><span class="pln"> </span><span class="pun">/</span><span class="pln"> </span><span class="lit">2</span><span class="pun">;</span></code></li><li class="L6"><code></code></li><li class="L7"><code><span class="pln">        mSort</span><span class="pun">(</span><span class="pln">arr</span><span class="pun">,</span><span class="pln"> left</span><span class="pun">,</span><span class="pln"> mid</span><span class="pun">);</span><span class="pln"> </span><span class="com">//递归排序左边</span></code></li><li class="L8"><code><span class="pln">        mSort</span><span class="pun">(</span><span class="pln">arr</span><span class="pun">,</span><span class="pln"> mid</span><span class="pun">+</span><span class="lit">1</span><span class="pun">,</span><span class="pln"> right</span><span class="pun">);</span><span class="pln"> </span><span class="com">//递归排序右边</span></code></li><li class="L9"><code><span class="pln">        merge</span><span class="pun">(</span><span class="pln">arr</span><span class="pun">,</span><span class="pln"> left</span><span class="pun">,</span><span class="pln"> mid</span><span class="pun">,</span><span class="pln"> right</span><span class="pun">);</span><span class="pln"> </span><span class="com">//合并</span></code></li><li class="L0"><code><span class="pln">    </span><span class="pun">}</span></code></li><li class="L1"><code></code></li><li class="L2"><code><span class="pln">    </span><span class="com">/**</span></code></li><li class="L3"><code><span class="com">     * 合并两个有序数组</span></code></li><li class="L4"><code><span class="com">     * @param arr 待合并数组</span></code></li><li class="L5"><code><span class="com">     * @param left 左指针</span></code></li><li class="L6"><code><span class="com">     * @param mid 中间指针</span></code></li><li class="L7"><code><span class="com">     * @param right 右指针</span></code></li><li class="L8"><code><span class="com">     */</span></code></li><li class="L9"><code><span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> merge</span><span class="pun">(</span><span class="kwd">int</span><span class="pun">[]</span><span class="pln"> arr</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> left</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> mid</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> right</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L0"><code><span class="pln">        </span><span class="com">//[left, mid] [mid+1, right]</span></code></li><li class="L1"><code><span class="pln">        </span><span class="kwd">int</span><span class="pun">[]</span><span class="pln"> temp </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="kwd">int</span><span class="pun">[</span><span class="pln">right </span><span class="pun">-</span><span class="pln"> left </span><span class="pun">+</span><span class="pln"> </span><span class="lit">1</span><span class="pun">];</span><span class="pln"> </span><span class="com">//中间数组</span></code></li><li class="L2"><code></code></li><li class="L3"><code><span class="pln">        </span><span class="kwd">int</span><span class="pln"> i </span><span class="pun">=</span><span class="pln"> left</span><span class="pun">;</span></code></li><li class="L4"><code><span class="pln">        </span><span class="kwd">int</span><span class="pln"> j </span><span class="pun">=</span><span class="pln"> mid </span><span class="pun">+</span><span class="pln"> </span><span class="lit">1</span><span class="pun">;</span></code></li><li class="L5"><code><span class="pln">        </span><span class="kwd">int</span><span class="pln"> k </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span></code></li><li class="L6"><code><span class="pln">        </span><span class="kwd">while</span><span class="pun">(</span><span class="pln">i </span><span class="pun">&lt;=</span><span class="pln"> mid </span><span class="pun">&amp;&amp;</span><span class="pln"> j </span><span class="pun">&lt;=</span><span class="pln"> right</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L7"><code><span class="pln">            </span><span class="kwd">if</span><span class="pun">(</span><span class="pln">arr</span><span class="pun">[</span><span class="pln">i</span><span class="pun">]</span><span class="pln"> </span><span class="pun">&lt;=</span><span class="pln"> arr</span><span class="pun">[</span><span class="pln">j</span><span class="pun">])</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L8"><code><span class="pln">                temp</span><span class="pun">[</span><span class="pln">k</span><span class="pun">++]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> arr</span><span class="pun">[</span><span class="pln">i</span><span class="pun">++];</span></code></li><li class="L9"><code><span class="pln">            </span><span class="pun">}</span></code></li><li class="L0"><code><span class="pln">            </span><span class="kwd">else</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L1"><code><span class="pln">                temp</span><span class="pun">[</span><span class="pln">k</span><span class="pun">++]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> arr</span><span class="pun">[</span><span class="pln">j</span><span class="pun">++];</span></code></li><li class="L2"><code><span class="pln">            </span><span class="pun">}</span></code></li><li class="L3"><code><span class="pln">        </span><span class="pun">}</span></code></li><li class="L4"><code></code></li><li class="L5"><code><span class="pln">        </span><span class="kwd">while</span><span class="pun">(</span><span class="pln">i </span><span class="pun">&lt;=</span><span class="pln"> mid</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L6"><code><span class="pln">            temp</span><span class="pun">[</span><span class="pln">k</span><span class="pun">++]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> arr</span><span class="pun">[</span><span class="pln">i</span><span class="pun">++];</span></code></li><li class="L7"><code><span class="pln">        </span><span class="pun">}</span></code></li><li class="L8"><code></code></li><li class="L9"><code><span class="pln">        </span><span class="kwd">while</span><span class="pun">(</span><span class="pln">j </span><span class="pun">&lt;=</span><span class="pln"> right</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L0"><code><span class="pln">            temp</span><span class="pun">[</span><span class="pln">k</span><span class="pun">++]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> arr</span><span class="pun">[</span><span class="pln">j</span><span class="pun">++];</span></code></li><li class="L1"><code><span class="pln">        </span><span class="pun">}</span></code></li><li class="L2"><code></code></li><li class="L3"><code><span class="pln">        </span><span class="kwd">for</span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> p</span><span class="pun">=</span><span class="lit">0</span><span class="pun">;</span><span class="pln"> p</span><span class="pun">&lt;</span><span class="pln">temp</span><span class="pun">.</span><span class="pln">length</span><span class="pun">;</span><span class="pln"> p</span><span class="pun">++)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L4"><code><span class="pln">            arr</span><span class="pun">[</span><span class="pln">left </span><span class="pun">+</span><span class="pln"> p</span><span class="pun">]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> temp</span><span class="pun">[</span><span class="pln">p</span><span class="pun">];</span></code></li><li class="L5"><code><span class="pln">        </span><span class="pun">}</span></code></li><li class="L6"><code></code></li><li class="L7"><code><span class="pln">    </span><span class="pun">}</span></code></li><li class="L8"><code><span class="pun">}</span></code></li></ol></pre><div class="md-section-divider"></div><h3 data-anchor-id="9zdo" id="计数排序">计数排序</h3><p data-anchor-id="yh9a">如果在面试中有面试官要求你写一个O(n)时间复杂度的排序算法，你千万不要立刻说：这不可能！虽然前面基于比较的排序的下限是O(nlogn)。但是确实也有线性时间复杂度的排序，只不过有前提条件，就是待排序的数要满足一定的范围的整数，而且计数排序需要比较多的辅助空间。其基本思想是，用待排序的数作为计数数组的下标，统计每个数字的个数。然后依次输出即可得到有序序列。</p><p data-anchor-id="pkr9">实现代码：</p><div class="md-section-divider"></div><pre class="prettyprint linenums prettyprinted" data-anchor-id="k5mu"><ol class="linenums"><li class="L0"><code><span class="kwd">public</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">CountSort</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L1"><code></code></li><li class="L2"><code><span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> countSort</span><span class="pun">(</span><span class="kwd">int</span><span class="pun">[]</span><span class="pln"> arr</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L3"><code><span class="pln">        </span><span class="kwd">if</span><span class="pun">(</span><span class="pln">arr </span><span class="pun">==</span><span class="pln"> </span><span class="kwd">null</span><span class="pln"> </span><span class="pun">||</span><span class="pln"> arr</span><span class="pun">.</span><span class="pln">length </span><span class="pun">==</span><span class="pln"> </span><span class="lit">0</span><span class="pun">)</span></code></li><li class="L4"><code><span class="pln">            </span><span class="kwd">return</span><span class="pln"> </span><span class="pun">;</span></code></li><li class="L5"><code></code></li><li class="L6"><code><span class="pln">        </span><span class="kwd">int</span><span class="pln"> max </span><span class="pun">=</span><span class="pln"> max</span><span class="pun">(</span><span class="pln">arr</span><span class="pun">);</span></code></li><li class="L7"><code></code></li><li class="L8"><code><span class="pln">        </span><span class="kwd">int</span><span class="pun">[]</span><span class="pln"> count </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="kwd">int</span><span class="pun">[</span><span class="pln">max</span><span class="pun">+</span><span class="lit">1</span><span class="pun">];</span></code></li><li class="L9"><code><span class="pln">        </span><span class="typ">Arrays</span><span class="pun">.</span><span class="pln">fill</span><span class="pun">(</span><span class="pln">count</span><span class="pun">,</span><span class="pln"> </span><span class="lit">0</span><span class="pun">);</span></code></li><li class="L0"><code></code></li><li class="L1"><code><span class="pln">        </span><span class="kwd">for</span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> i</span><span class="pun">=</span><span class="lit">0</span><span class="pun">;</span><span class="pln"> i</span><span class="pun">&lt;</span><span class="pln">arr</span><span class="pun">.</span><span class="pln">length</span><span class="pun">;</span><span class="pln"> i</span><span class="pun">++)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L2"><code><span class="pln">            count</span><span class="pun">[</span><span class="pln">arr</span><span class="pun">[</span><span class="pln">i</span><span class="pun">]]</span><span class="pln"> </span><span class="pun">++;</span></code></li><li class="L3"><code><span class="pln">        </span><span class="pun">}</span></code></li><li class="L4"><code></code></li><li class="L5"><code><span class="pln">        </span><span class="kwd">int</span><span class="pln"> k </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span></code></li><li class="L6"><code><span class="pln">        </span><span class="kwd">for</span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> i</span><span class="pun">=</span><span class="lit">0</span><span class="pun">;</span><span class="pln"> i</span><span class="pun">&lt;=</span><span class="pln">max</span><span class="pun">;</span><span class="pln"> i</span><span class="pun">++)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L7"><code><span class="pln">            </span><span class="kwd">for</span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> j</span><span class="pun">=</span><span class="lit">0</span><span class="pun">;</span><span class="pln"> j</span><span class="pun">&lt;</span><span class="pln">count</span><span class="pun">[</span><span class="pln">i</span><span class="pun">];</span><span class="pln"> j</span><span class="pun">++)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L8"><code><span class="pln">                arr</span><span class="pun">[</span><span class="pln">k</span><span class="pun">++]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> i</span><span class="pun">;</span></code></li><li class="L9"><code><span class="pln">            </span><span class="pun">}</span></code></li><li class="L0"><code><span class="pln">        </span><span class="pun">}</span></code></li><li class="L1"><code></code></li><li class="L2"><code><span class="pln">    </span><span class="pun">}</span></code></li><li class="L3"><code></code></li><li class="L4"><code><span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> max</span><span class="pun">(</span><span class="kwd">int</span><span class="pun">[]</span><span class="pln"> arr</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L5"><code><span class="pln">        </span><span class="kwd">int</span><span class="pln"> max </span><span class="pun">=</span><span class="pln"> </span><span class="typ">Integer</span><span class="pun">.</span><span class="pln">MIN_VALUE</span><span class="pun">;</span></code></li><li class="L6"><code><span class="pln">        </span><span class="kwd">for</span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> ele </span><span class="pun">:</span><span class="pln"> arr</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L7"><code><span class="pln">            </span><span class="kwd">if</span><span class="pun">(</span><span class="pln">ele </span><span class="pun">&gt;</span><span class="pln"> max</span><span class="pun">)</span></code></li><li class="L8"><code><span class="pln">                max </span><span class="pun">=</span><span class="pln"> ele</span><span class="pun">;</span></code></li><li class="L9"><code><span class="pln">        </span><span class="pun">}</span></code></li><li class="L0"><code></code></li><li class="L1"><code><span class="pln">        </span><span class="kwd">return</span><span class="pln"> max</span><span class="pun">;</span></code></li><li class="L2"><code><span class="pln">    </span><span class="pun">}</span></code></li><li class="L3"><code></code></li><li class="L4"><code><span class="pun">}</span></code></li></ol></pre><div class="md-section-divider"></div><h3 data-anchor-id="p1u5" id="桶排序">桶排序</h3><p data-anchor-id="cbdy">桶排序算是计数排序的一种改进和推广，但是网上有许多资料把计数排序和桶排序混为一谈。其实桶排序要比计数排序复杂许多。</p><p data-anchor-id="8zpc">对桶排序的分析和解释借鉴这位兄弟的文章（有改动）：<a href="http://hxraid.iteye.com/blog/647759" target="_blank">http://hxraid.iteye.com/blog/647759</a></p><p data-anchor-id="mme2">桶排序的基本思想：</p><p data-anchor-id="tjn7">假设有一组长度为N的待排关键字序列K[1....n]。首先将这个序列划分成M个的子区间(桶) 。然后基于某种映射函数 ，将待排序列的关键字k映射到第i个桶中(即桶数组B的下标 i) ，那么该关键字k就作为B[i]中的元素(每个桶B[i]都是一组大小为N/M的序列)。接着对每个桶B[i]中的所有元素进行比较排序(可以使用快排)。然后依次枚举输出B[0]....B[M]中的全部内容即是一个有序序列。bindex=f(key)其中，bindex 为桶数组B的下标(即第bindex个桶), k为待排序列的关键字。桶排序之所以能够高效，其关键在于这个映射函数，它必须做到：如果关键字k1

</p><p data-anchor-id="ja7k">也就是说B(i)中的最小数据都要大于B(i-1)中最大数据。很显然，映射函数的确定与数据本身的特点有很大的关系。</p><p data-anchor-id="h1su">举个栗子： <br>
<img src="http://static.zybuluo.com/homiss/jlz4w669wrr85pxnj8hb1ky3/image_1bk443hf11c9a4ng1sut10kodml2a.png" alt="image_1bk443hf11c9a4ng1sut10kodml2a.png-5.6kB" title=""></p><p data-anchor-id="m0ht">假如待排序列K={49、38、35、97、76、73、27、49}。这些数据全部在1—100之间。因此我们定制10个桶，然后确定映射函数f(k)=k/10。则第一个关键字49将定位到第4个桶中(49/10=4)。依次将所有关键字全部堆入桶中，并在每个非空的桶中进行快速排序后得到如图所示。只要顺序输出每个B[i]中的数据就可以得到有序序列了。</p><p data-anchor-id="oo1b">桶排序分析：</p><p data-anchor-id="rhpj">桶排序利用函数的映射关系，减少了几乎所有的比较工作。实际上，桶排序的f(k)值的计算，其作用就相当于快排中划分，希尔排序中的子序列，归并排序中的子问题，已经把大量数据分割成了基本有序的数据块(桶)。然后只需要对桶中的少量数据做先进的比较排序即可。　</p><p data-anchor-id="dgqw">对N个关键字进行桶排序的时间复杂度分为两个部分：</p><p data-anchor-id="pdj4">(1) 循环计算每个关键字的桶映射函数，这个时间复杂度是O(N)。</p><p data-anchor-id="urdu">(2) 利用先进的比较排序算法对每个桶内的所有数据进行排序，其时间复杂度为∑ O(Ni*logNi) 。其中Ni 为第i个桶的数据量。</p><p data-anchor-id="lurf">很显然，第(2)部分是桶排序性能好坏的决定因素。尽量减少桶内数据的数量是提高效率的唯一办法(因为基于比较排序的最好平均时间复杂度只能达到O(N*logN)了)。因此，我们需要尽量做到下面两点：</p><p data-anchor-id="qqi1">(1) 映射函数f(k)能够将N个数据平均的分配到M个桶中，这样每个桶就有[N/M]个数据量。</p><p data-anchor-id="9ij8">(2) 尽量的增大桶的数量。极限情况下每个桶只能得到一个数据，这样就完全避开了桶内数据的“比较”排序操作。当然，做到这一点很不容易，数据量巨大的情况下，f(k)函数会使得桶集合的数量巨大，空间浪费严重。这就是一个时间代价和空间代价的权衡问题了。</p><p data-anchor-id="wwkf">对于N个待排数据，M个桶，平均每个桶[N/M]个数据的桶排序平均时间复杂度为：</p><p data-anchor-id="nbww">O(N)+O(M*(N/M)<em>log(N/M))=O(N+N</em>(logN-logM))=O(N+N*logN-N*logM) <br>
当N=M时，即极限情况下每个桶只有一个数据时。桶排序的最好效率能够达到O(N)。</p><p data-anchor-id="y1n4">总结： 桶排序的平均时间复杂度为线性的O(N+C)，其中C=N*(logN-logM)。如果相对于同样的N，桶数量M越大，其效率越高，最好的时间复杂度达到O(N)。 当然桶排序的空间复杂度为O(N+M)，如果输入数据非常庞大，而桶的数量也非常多，则空间代价无疑是昂贵的。此外，桶排序是稳定的。</p><p data-anchor-id="pz2d">实现代码：</p><div class="md-section-divider"></div><pre class="prettyprint linenums prettyprinted" data-anchor-id="dg2f"><ol class="linenums"><li class="L0"><code><span class="kwd">public</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">BucketSort</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L1"><code></code></li><li class="L2"><code><span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> bucketSort</span><span class="pun">(</span><span class="kwd">int</span><span class="pun">[]</span><span class="pln"> arr</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L3"><code><span class="pln">        </span><span class="kwd">if</span><span class="pun">(</span><span class="pln">arr </span><span class="pun">==</span><span class="pln"> </span><span class="kwd">null</span><span class="pln"> </span><span class="pun">&amp;&amp;</span><span class="pln"> arr</span><span class="pun">.</span><span class="pln">length </span><span class="pun">==</span><span class="pln"> </span><span class="lit">0</span><span class="pun">)</span></code></li><li class="L4"><code><span class="pln">            </span><span class="kwd">return</span><span class="pln"> </span><span class="pun">;</span></code></li><li class="L5"><code></code></li><li class="L6"><code><span class="pln">        </span><span class="kwd">int</span><span class="pln"> bucketNums </span><span class="pun">=</span><span class="pln"> </span><span class="lit">10</span><span class="pun">;</span><span class="pln"> </span><span class="com">//这里默认为10，规定待排数[0,100)</span></code></li><li class="L7"><code><span class="pln">        </span><span class="typ">List</span><span class="pun">&lt;</span><span class="typ">List</span><span class="pun">&lt;</span><span class="typ">Integer</span><span class="pun">&gt;&gt;</span><span class="pln"> buckets </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">ArrayList</span><span class="pun">&lt;</span><span class="typ">List</span><span class="pun">&lt;</span><span class="typ">Integer</span><span class="pun">&gt;&gt;();</span><span class="pln"> </span><span class="com">//桶的索引</span></code></li><li class="L8"><code></code></li><li class="L9"><code><span class="pln">        </span><span class="kwd">for</span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> i</span><span class="pun">=</span><span class="lit">0</span><span class="pun">;</span><span class="pln"> i</span><span class="pun">&lt;</span><span class="lit">10</span><span class="pun">;</span><span class="pln"> i</span><span class="pun">++)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L0"><code><span class="pln">            buckets</span><span class="pun">.</span><span class="pln">add</span><span class="pun">(</span><span class="kwd">new</span><span class="pln"> </span><span class="typ">LinkedList</span><span class="pun">&lt;</span><span class="typ">Integer</span><span class="pun">&gt;());</span><span class="pln"> </span><span class="com">//用链表比较合适</span></code></li><li class="L1"><code><span class="pln">        </span><span class="pun">}</span></code></li><li class="L2"><code></code></li><li class="L3"><code><span class="pln">        </span><span class="com">//划分桶</span></code></li><li class="L4"><code><span class="pln">        </span><span class="kwd">for</span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> i</span><span class="pun">=</span><span class="lit">0</span><span class="pun">;</span><span class="pln"> i</span><span class="pun">&lt;</span><span class="pln">arr</span><span class="pun">.</span><span class="pln">length</span><span class="pun">;</span><span class="pln"> i</span><span class="pun">++)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L5"><code><span class="pln">            buckets</span><span class="pun">.</span><span class="kwd">get</span><span class="pun">(</span><span class="pln">f</span><span class="pun">(</span><span class="pln">arr</span><span class="pun">[</span><span class="pln">i</span><span class="pun">])).</span><span class="pln">add</span><span class="pun">(</span><span class="pln">arr</span><span class="pun">[</span><span class="pln">i</span><span class="pun">]);</span></code></li><li class="L6"><code><span class="pln">        </span><span class="pun">}</span></code></li><li class="L7"><code></code></li><li class="L8"><code><span class="pln">        </span><span class="com">//对每个桶进行排序</span></code></li><li class="L9"><code><span class="pln">        </span><span class="kwd">for</span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> i</span><span class="pun">=</span><span class="lit">0</span><span class="pun">;</span><span class="pln"> i</span><span class="pun">&lt;</span><span class="pln">buckets</span><span class="pun">.</span><span class="pln">size</span><span class="pun">();</span><span class="pln"> i</span><span class="pun">++)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L0"><code><span class="pln">            </span><span class="kwd">if</span><span class="pun">(!</span><span class="pln">buckets</span><span class="pun">.</span><span class="kwd">get</span><span class="pun">(</span><span class="pln">i</span><span class="pun">).</span><span class="pln">isEmpty</span><span class="pun">())</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L1"><code><span class="pln">                </span><span class="typ">Collections</span><span class="pun">.</span><span class="pln">sort</span><span class="pun">(</span><span class="pln">buckets</span><span class="pun">.</span><span class="kwd">get</span><span class="pun">(</span><span class="pln">i</span><span class="pun">));</span><span class="pln"> </span><span class="com">//对每个桶进行快排</span></code></li><li class="L2"><code><span class="pln">            </span><span class="pun">}</span></code></li><li class="L3"><code><span class="pln">        </span><span class="pun">}</span></code></li><li class="L4"><code></code></li><li class="L5"><code><span class="pln">        </span><span class="com">//还原排好序的数组</span></code></li><li class="L6"><code><span class="pln">        </span><span class="kwd">int</span><span class="pln"> k </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span></code></li><li class="L7"><code><span class="pln">        </span><span class="kwd">for</span><span class="pun">(</span><span class="typ">List</span><span class="pun">&lt;</span><span class="typ">Integer</span><span class="pun">&gt;</span><span class="pln"> bucket </span><span class="pun">:</span><span class="pln"> buckets</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L8"><code><span class="pln">            </span><span class="kwd">for</span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> ele </span><span class="pun">:</span><span class="pln"> bucket</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L9"><code><span class="pln">                arr</span><span class="pun">[</span><span class="pln">k</span><span class="pun">++]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> ele</span><span class="pun">;</span></code></li><li class="L0"><code><span class="pln">            </span><span class="pun">}</span></code></li><li class="L1"><code><span class="pln">        </span><span class="pun">}</span></code></li><li class="L2"><code><span class="pln">    </span><span class="pun">}</span></code></li><li class="L3"><code></code></li><li class="L4"><code><span class="pln">    </span><span class="com">/**</span></code></li><li class="L5"><code><span class="com">     * 映射函数</span></code></li><li class="L6"><code><span class="com">     * @param x</span></code></li><li class="L7"><code><span class="com">     * @return</span></code></li><li class="L8"><code><span class="com">     */</span></code></li><li class="L9"><code><span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> f</span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> x</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L0"><code><span class="pln">        </span><span class="kwd">return</span><span class="pln"> x </span><span class="pun">/</span><span class="pln"> </span><span class="lit">10</span><span class="pun">;</span></code></li><li class="L1"><code><span class="pln">    </span><span class="pun">}</span></code></li><li class="L2"><code></code></li><li class="L3"><code><span class="pun">}</span></code></li></ol></pre><div class="md-section-divider"></div><h3 data-anchor-id="11a5" id="基数排序">基数排序</h3><p data-anchor-id="xqmg">基数排序又是一种和前面排序方式不同的排序方式，基数排序不需要进行记录关键字之间的比较。基数排序是一种借助多关键字排序思想对单逻辑关键字进行排序的方法。所谓的多关键字排序就是有多个优先级不同的关键字。比如说成绩的排序，如果两个人总分相同，则语文高的排在前面，语文成绩也相同则数学高的排在前面。。。如果对数字进行排序，那么个位、十位、百位就是不同优先级的关键字，如果要进行升序排序，那么个位、十位、百位优先级一次增加。基数排序是通过多次的收分配和收集来实现的，关键字优先级低的先进行分配和收集。</p><p data-anchor-id="uiq2">举个栗子： <br>
<img src="http://static.zybuluo.com/homiss/e7upib3otou6sxr0khwmqldk/image_1bk444jv219053vk1fvnk7jrpp2n.png" alt="image_1bk444jv219053vk1fvnk7jrpp2n.png-230.7kB" title=""> <br>
<img src="http://static.zybuluo.com/homiss/a8o1b0jxdwfves16ccoyhe1e/image_1bk4451i8s0u1i5h1qklafgvm34.png" alt="image_1bk4451i8s0u1i5h1qklafgvm34.png-145.5kB" title=""></p><p data-anchor-id="0gwk">实现代码：</p><div class="md-section-divider"></div><pre class="prettyprint linenums prettyprinted" data-anchor-id="2opo"><ol class="linenums"><li class="L0"><code class="language-java"><span class="kwd">public</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">RadixSort</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L1"><code class="language-java"></code></li><li class="L2"><code class="language-java"><span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> radixSort</span><span class="pun">(</span><span class="kwd">int</span><span class="pun">[]</span><span class="pln"> arr</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L3"><code class="language-java"><span class="pln">        </span><span class="kwd">if</span><span class="pun">(</span><span class="pln">arr </span><span class="pun">==</span><span class="pln"> </span><span class="kwd">null</span><span class="pln"> </span><span class="pun">&amp;&amp;</span><span class="pln"> arr</span><span class="pun">.</span><span class="pln">length </span><span class="pun">==</span><span class="pln"> </span><span class="lit">0</span><span class="pun">)</span></code></li><li class="L4"><code class="language-java"><span class="pln">            </span><span class="kwd">return</span><span class="pln"> </span><span class="pun">;</span></code></li><li class="L5"><code class="language-java"></code></li><li class="L6"><code class="language-java"><span class="pln">        </span><span class="kwd">int</span><span class="pln"> maxBit </span><span class="pun">=</span><span class="pln"> getMaxBit</span><span class="pun">(</span><span class="pln">arr</span><span class="pun">);</span></code></li><li class="L7"><code class="language-java"></code></li><li class="L8"><code class="language-java"></code></li><li class="L9"><code class="language-java"><span class="pln">        </span><span class="kwd">for</span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> i</span><span class="pun">=</span><span class="lit">1</span><span class="pun">;</span><span class="pln"> i</span><span class="pun">&lt;=</span><span class="pln">maxBit</span><span class="pun">;</span><span class="pln"> i</span><span class="pun">++)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L0"><code class="language-java"></code></li><li class="L1"><code class="language-java"><span class="pln">            </span><span class="typ">List</span><span class="pun">&lt;</span><span class="typ">List</span><span class="pun">&lt;</span><span class="typ">Integer</span><span class="pun">&gt;&gt;</span><span class="pln"> buf </span><span class="pun">=</span><span class="pln"> distribute</span><span class="pun">(</span><span class="pln">arr</span><span class="pun">,</span><span class="pln"> i</span><span class="pun">);</span><span class="pln"> </span><span class="com">//分配</span></code></li><li class="L2"><code class="language-java"><span class="pln">            collecte</span><span class="pun">(</span><span class="pln">arr</span><span class="pun">,</span><span class="pln"> buf</span><span class="pun">);</span><span class="pln"> </span><span class="com">//收集</span></code></li><li class="L3"><code class="language-java"><span class="pln">        </span><span class="pun">}</span></code></li><li class="L4"><code class="language-java"></code></li><li class="L5"><code class="language-java"><span class="pln">    </span><span class="pun">}</span></code></li><li class="L6"><code class="language-java"></code></li><li class="L7"><code class="language-java"><span class="pln">    </span><span class="com">/**</span></code></li><li class="L8"><code class="language-java"><span class="com">     * 分配</span></code></li><li class="L9"><code class="language-java"><span class="com">     * @param arr 待分配数组</span></code></li><li class="L0"><code class="language-java"><span class="com">     * @param iBit 要分配第几位</span></code></li><li class="L1"><code class="language-java"><span class="com">     * @return</span></code></li><li class="L2"><code class="language-java"><span class="com">     */</span></code></li><li class="L3"><code class="language-java"><span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="typ">List</span><span class="pun">&lt;</span><span class="typ">List</span><span class="pun">&lt;</span><span class="typ">Integer</span><span class="pun">&gt;&gt;</span><span class="pln"> distribute</span><span class="pun">(</span><span class="kwd">int</span><span class="pun">[]</span><span class="pln"> arr</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> iBit</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L4"><code class="language-java"><span class="pln">        </span><span class="typ">List</span><span class="pun">&lt;</span><span class="typ">List</span><span class="pun">&lt;</span><span class="typ">Integer</span><span class="pun">&gt;&gt;</span><span class="pln"> buf </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">ArrayList</span><span class="pun">&lt;</span><span class="typ">List</span><span class="pun">&lt;</span><span class="typ">Integer</span><span class="pun">&gt;&gt;();</span></code></li><li class="L5"><code class="language-java"><span class="pln">        </span><span class="kwd">for</span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> j</span><span class="pun">=</span><span class="lit">0</span><span class="pun">;</span><span class="pln"> j</span><span class="pun">&lt;</span><span class="lit">10</span><span class="pun">;</span><span class="pln"> j</span><span class="pun">++)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L6"><code class="language-java"><span class="pln">            buf</span><span class="pun">.</span><span class="pln">add</span><span class="pun">(</span><span class="kwd">new</span><span class="pln"> </span><span class="typ">LinkedList</span><span class="pun">&lt;</span><span class="typ">Integer</span><span class="pun">&gt;());</span></code></li><li class="L7"><code class="language-java"><span class="pln">        </span><span class="pun">}</span></code></li><li class="L8"><code class="language-java"><span class="pln">        </span><span class="kwd">for</span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> i</span><span class="pun">=</span><span class="lit">0</span><span class="pun">;</span><span class="pln"> i</span><span class="pun">&lt;</span><span class="pln">arr</span><span class="pun">.</span><span class="pln">length</span><span class="pun">;</span><span class="pln"> i</span><span class="pun">++)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L9"><code class="language-java"><span class="pln">            buf</span><span class="pun">.</span><span class="pln">get</span><span class="pun">(</span><span class="pln">getNBit</span><span class="pun">(</span><span class="pln">arr</span><span class="pun">[</span><span class="pln">i</span><span class="pun">],</span><span class="pln"> iBit</span><span class="pun">)).</span><span class="pln">add</span><span class="pun">(</span><span class="pln">arr</span><span class="pun">[</span><span class="pln">i</span><span class="pun">]);</span></code></li><li class="L0"><code class="language-java"><span class="pln">        </span><span class="pun">}</span></code></li><li class="L1"><code class="language-java"><span class="pln">        </span><span class="kwd">return</span><span class="pln"> buf</span><span class="pun">;</span></code></li><li class="L2"><code class="language-java"><span class="pln">    </span><span class="pun">}</span></code></li><li class="L3"><code class="language-java"></code></li><li class="L4"><code class="language-java"><span class="pln">    </span><span class="com">/**</span></code></li><li class="L5"><code class="language-java"><span class="com">     * 收集</span></code></li><li class="L6"><code class="language-java"><span class="com">     * @param arr 把分配的数据收集到arr中</span></code></li><li class="L7"><code class="language-java"><span class="com">     * @param buf </span></code></li><li class="L8"><code class="language-java"><span class="com">     */</span></code></li><li class="L9"><code class="language-java"><span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> collecte</span><span class="pun">(</span><span class="kwd">int</span><span class="pun">[]</span><span class="pln"> arr</span><span class="pun">,</span><span class="pln"> </span><span class="typ">List</span><span class="pun">&lt;</span><span class="typ">List</span><span class="pun">&lt;</span><span class="typ">Integer</span><span class="pun">&gt;&gt;</span><span class="pln"> buf</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L0"><code class="language-java"><span class="pln">        </span><span class="kwd">int</span><span class="pln"> k </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span></code></li><li class="L1"><code class="language-java"><span class="pln">        </span><span class="kwd">for</span><span class="pun">(</span><span class="typ">List</span><span class="pun">&lt;</span><span class="typ">Integer</span><span class="pun">&gt;</span><span class="pln"> bucket </span><span class="pun">:</span><span class="pln"> buf</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L2"><code class="language-java"><span class="pln">            </span><span class="kwd">for</span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> ele </span><span class="pun">:</span><span class="pln"> bucket</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L3"><code class="language-java"><span class="pln">                arr</span><span class="pun">[</span><span class="pln">k</span><span class="pun">++]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> ele</span><span class="pun">;</span></code></li><li class="L4"><code class="language-java"><span class="pln">            </span><span class="pun">}</span></code></li><li class="L5"><code class="language-java"><span class="pln">        </span><span class="pun">}</span></code></li><li class="L6"><code class="language-java"></code></li><li class="L7"><code class="language-java"></code></li><li class="L8"><code class="language-java"><span class="pln">    </span><span class="pun">}</span></code></li><li class="L9"><code class="language-java"></code></li><li class="L0"><code class="language-java"><span class="pln">    </span><span class="com">/**</span></code></li><li class="L1"><code class="language-java"><span class="com">     * 获取最大位数</span></code></li><li class="L2"><code class="language-java"><span class="com">     * @param x</span></code></li><li class="L3"><code class="language-java"><span class="com">     * @return</span></code></li><li class="L4"><code class="language-java"><span class="com">     */</span></code></li><li class="L5"><code class="language-java"><span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> getMaxBit</span><span class="pun">(</span><span class="kwd">int</span><span class="pun">[]</span><span class="pln"> arr</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L6"><code class="language-java"><span class="pln">        </span><span class="kwd">int</span><span class="pln"> max </span><span class="pun">=</span><span class="pln"> </span><span class="typ">Integer</span><span class="pun">.</span><span class="pln">MIN_VALUE</span><span class="pun">;</span></code></li><li class="L7"><code class="language-java"><span class="pln">        </span><span class="kwd">for</span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> ele </span><span class="pun">:</span><span class="pln"> arr</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L8"><code class="language-java"><span class="pln">            </span><span class="kwd">int</span><span class="pln"> len </span><span class="pun">=</span><span class="pln"> </span><span class="pun">(</span><span class="pln">ele</span><span class="pun">+</span><span class="str">""</span><span class="pun">).</span><span class="pln">length</span><span class="pun">();</span></code></li><li class="L9"><code class="language-java"><span class="pln">            </span><span class="kwd">if</span><span class="pun">(</span><span class="pln">len </span><span class="pun">&gt;</span><span class="pln"> max</span><span class="pun">)</span></code></li><li class="L0"><code class="language-java"><span class="pln">                max </span><span class="pun">=</span><span class="pln"> len</span><span class="pun">;</span></code></li><li class="L1"><code class="language-java"><span class="pln">        </span><span class="pun">}</span></code></li><li class="L2"><code class="language-java"><span class="pln">        </span><span class="kwd">return</span><span class="pln"> max</span><span class="pun">;</span></code></li><li class="L3"><code class="language-java"><span class="pln">    </span><span class="pun">}</span></code></li><li class="L4"><code class="language-java"></code></li><li class="L5"><code class="language-java"><span class="pln">    </span><span class="com">/**</span></code></li><li class="L6"><code class="language-java"><span class="com">     * 获取x的第n位，如果没有则为0.</span></code></li><li class="L7"><code class="language-java"><span class="com">     * @param x</span></code></li><li class="L8"><code class="language-java"><span class="com">     * @param n</span></code></li><li class="L9"><code class="language-java"><span class="com">     * @return</span></code></li><li class="L0"><code class="language-java"><span class="com">     */</span></code></li><li class="L1"><code class="language-java"><span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> getNBit</span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> x</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> n</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L2"><code class="language-java"></code></li><li class="L3"><code class="language-java"><span class="pln">        </span><span class="typ">String</span><span class="pln"> sx </span><span class="pun">=</span><span class="pln"> x </span><span class="pun">+</span><span class="pln"> </span><span class="str">""</span><span class="pun">;</span></code></li><li class="L4"><code class="language-java"><span class="pln">        </span><span class="kwd">if</span><span class="pun">(</span><span class="pln">sx</span><span class="pun">.</span><span class="pln">length</span><span class="pun">()</span><span class="pln"> </span><span class="pun">&lt;</span><span class="pln"> n</span><span class="pun">)</span></code></li><li class="L5"><code class="language-java"><span class="pln">            </span><span class="kwd">return</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span></code></li><li class="L6"><code class="language-java"><span class="pln">        </span><span class="kwd">else</span></code></li><li class="L7"><code class="language-java"><span class="pln">            </span><span class="kwd">return</span><span class="pln"> sx</span><span class="pun">.</span><span class="pln">charAt</span><span class="pun">(</span><span class="pln">sx</span><span class="pun">.</span><span class="pln">length</span><span class="pun">()-</span><span class="pln">n</span><span class="pun">)</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> </span><span class="str">'0'</span><span class="pun">;</span></code></li><li class="L8"><code class="language-java"><span class="pln">    </span><span class="pun">}</span></code></li><li class="L9"><code class="language-java"></code></li><li class="L0"><code class="language-java"><span class="pun">}</span></code></li></ol></pre><div class="md-section-divider"></div><h3 data-anchor-id="1gpc" id="排序算法的各自的使用场景和适用场合">排序算法的各自的使用场景和适用场合。</h3><p data-anchor-id="54ih"><img src="http://static.zybuluo.com/homiss/t9dq56b62jeous319fn717xp/image_1bk446mjdb8k1nu71p2l1nk31ss83u.png" alt="image_1bk446mjdb8k1nu71p2l1nk31ss83u.png-119.3kB" title=""> <br>
1. 从平均时间来看，快速排序是效率最高的，但快速排序在最坏情况下的时间性能不如堆排序和归并排序。而后者相比较的结果是，在n较大时归并排序使用时间较少，但使用辅助空间较多。</p><ol data-anchor-id="nzae">
<li><p>上面说的简单排序包括除希尔排序之外的所有冒泡排序、插入排序、简单选择排序。其中直接插入排序最简单，但序列基本有序或者n较小时，直接插入排序是好的方法，因此常将它和其他的排序方法，如快速排序、归并排序等结合在一起使用。</p></li>
<li><p>基数排序的时间复杂度也可以写成O(d*n)。因此它最使用于n值很大而关键字较小的的序列。若关键字也很大，而序列中大多数记录的最高关键字均不同，则亦可先按最高关键字不同，将序列分成若干小的子序列，而后进行直接插入排序。</p></li>
<li><p>从方法的稳定性来比较，基数排序是稳定的内排方法，所有时间复杂度为O(n^2)的简单排序也是稳定的。但是快速排序、堆排序、希尔排序等时间性能较好的排序方法都是不稳定的。稳定性需要根据具体需求选择。</p></li>
<li><p>上面的算法实现大多数是使用线性存储结构，像插入排序这种算法用链表实现更好，省去了移动元素的时间。具体的存储结构在具体的实现版本中也是不同的。</p></li>
</ol><p data-anchor-id="96ej">附：基于比较排序算法时间下限为O(nlogn)的证明：</p><p data-anchor-id="p121">基于比较排序下限的证明是通过决策树证明的，决策树的高度Ω（nlgn），这样就得出了比较排序的下限。</p><p data-anchor-id="ff9o"><img src="http://static.zybuluo.com/homiss/i2kxwm3lk39wonlr6tc00033/image_1bk446bqbtog4n415lp198tk2r3h.png" alt="image_1bk446bqbtog4n415lp198tk2r3h.png-199.8kB" title=""></p><p data-anchor-id="w3qv">首先要引入决策树。 首先决策树是一颗二叉树，每个节点表示元素之间一组可能的排序，它予以京进行的比较相一致，比较的结果是树的边。 先来说明一些二叉树的性质，令T是深度为d的二叉树，则T最多有2^片树叶。 具有L片树叶的二叉树的深度至少是logL。 所以，对n个元素排序的决策树必然有n!片树叶（因为n个数有n!种不同的大小关系），所以决策树的深度至少是log(n!)，即至少需要log(n!)次比较。 而 log(n!)=logn+log(n-1)+log(n-2)+...+log2+log1 &gt;=logn+log(n-1)+log(n-2)+...+log(n/2) &gt;=(n/2)log(n/2) &gt;=(n/2)logn-n/2 =O(nlogn)所以只用到比较的排序算法最低时间复杂度是O(nlogn)。</p><div class="md-section-divider"></div><h3 data-anchor-id="1n4m" id="结束">结束</h3></div>
</body>
</html>